﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Yuebon.Car.Application;
using Yuebon.Car.Dtos;
using Yuebon.CMS.Application;
using Yuebon.Commons.Cache;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Json;
using Yuebon.Commons.Models;
using Yuebon.Security.Models;
using Yuebon.Shop.Application;
using Yuebon.Shop.Dtos;
using Yuebon.WebApi.Models;

namespace Yuebon.WebApi.Controllers
{
    /// <summary>
    /// 首页
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class HomeController : BaseController
    {
        /// <summary>
        /// 首页加载数据
        /// </summary>
        /// <param name="shopId">分销店铺Id</param>
        /// <param name="token">token令牌</param>
        /// <returns></returns>
        [HttpGet("GetHomeTopData")]
        public IActionResult GetHomeTopData(string shopId,string token)
        {
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                HomeViewModel homeViewModelResult = new HomeViewModel();
                if (string.IsNullOrEmpty(shopId) || shopId == "undefined")
                {
                    shopId = "201905131607366674";
                }
                ReferralsDto referrals = new ReferralsApp().GetById(shopId);
                YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
                homeViewModelResult = JsonConvert.DeserializeObject<HomeViewModel>(yuebonCacheHelper.Get("HomeTopData_"+ shopId).ToJson());
                if (homeViewModelResult == null)
                {
                    HomeViewModel homeViewModel = new HomeViewModel();
                    AutoDetailsApp autoDetailsApp = new AutoDetailsApp();
                    homeViewModel.HotAutoDetailsList = autoDetailsApp.FindCarListByRecommend("IsHot",referrals.MemberId);
                    homeViewModel.RedAutoDetailsList = autoDetailsApp.FindCarListByRecommend("IsRed", referrals.MemberId);
                    homeViewModel.TopAutoDetailsList = autoDetailsApp.FindCarListByRecommend("IsTop", referrals.MemberId);
                    AutoBrandApp autoBrandApp = new AutoBrandApp();
                    homeViewModel.TopAutoBrandList = autoBrandApp.FindAutoBrandListByIsTop();
                    ReferralsApp referralsApp = new ReferralsApp();
                    
                    homeViewModel.ReferralsShop = referralsApp.GetById(shopId);
                    SysSetting sysSetting = JsonConvert.DeserializeObject<SysSetting>(yuebonCacheHelper.Get("SysSetting").ToJson());
                  
                    homeViewModel.SysSettingHome = sysSetting;
                    BannerApp bannerApp = new BannerApp();
                    homeViewModel.BannerList = bannerApp.GetListWhere("AdId='201905061308017930'");
                    homeViewModel.Banner1= bannerApp.GetWhere("AdId='201905071952156991'");
                    homeViewModel.Banner2= bannerApp.GetWhere("AdId='201905071952388047'");
                    yuebonCacheHelper.Add("HomeTopData_"+ shopId, homeViewModel, new TimeSpan(0, 30, 0));
                    homeViewModelResult = homeViewModel;
                }
                if (homeViewModelResult != null)
                {
                    result.ResData = homeViewModelResult;
                    result.Success = true;
                }
                else
                {
                    result.ErrMsg = ErrCode.err60001;
                    result.ErrCode = "60001";
                }
            }
            return ToJsonContent(result);
        }
    }
}